Skip to content

Standardise Type Hints, Simplify Overloads, and Refactor Core Utilities#39

Merged
chrimaho merged 7 commits into
mainfrom
updates
Jan 2, 2026
Merged

Standardise Type Hints, Simplify Overloads, and Refactor Core Utilities#39
chrimaho merged 7 commits into
mainfrom
updates

Conversation

@chrimaho

@chrimaho chrimaho commented Jan 2, 2026

Copy link
Copy Markdown
Contributor

📋 Overview

This pull request implements a comprehensive standardisation of type hints across the entire codebase, transitioning from custom type aliases to standard Python typing constructs. By leveraging collections.abc.Collection and explicit generic types like list[str], the project achieves better compatibility with modern static analysis tools and improves overall code clarity. Additionally, this PR simplifies complex function overloads, relocates constants to more appropriate modules, and enhances documentation for core classes.

🛠️ Type Hint and Import Standardisation

Transition to Standard Library Types

  • checkers.py: Replace custom any_collection and str_collection aliases with Collection[Any] and Collection[str] to align with standard Python protocols
  • bools.py: Update strtobool() to use list[str] for internal truthy and falsy value tracking
  • collection_types.py: Remove redundant type aliases and relocate the log_levels literal to the output.py module where it is primarily utilised
  • defaults.py: Standardise the __all__ export list and internal validation lists to use the native list[str] type
  • dictionaries.py: Refactor dict_reverse_keys_and_values() to use explicit dict[Any, Any] and dict[str, Any] type hints
  • lists.py: Update flatten() and flat_list() to return standard list[Any] instead of custom list aliases
  • strings.py: Replace str_list_tuple with Union[list[str], tuple[str, ...]] in string membership functions for better transparency

Dependency Cleanup

  • Imports: Remove numerous imports from toolbox_python.collection_types across all modules, reducing internal coupling and simplifying the dependency graph
  • Aliases: Add explicit Callable[..., bool] and Callable[..., None] type annotations to function aliases in checkers.py to ensure they are correctly recognised by type checkers

⚙️ Function Logic and Overload Simplification

Streamlined Type Checking

  • is_value_of_type(): Simplify the function signature by using Union[type, Collection[type]], allowing for a more robust handling of both single types and collections of types
  • is_all_values_of_type(): Reduce the number of @overload definitions by consolidating input types into Collection[Any] and Collection[type]
  • assert_value_of_type(): Refactor assertion logic to match the simplified checking patterns, ensuring consistent behaviour across the validation suite
  • Internal Logic: Improve the conversion of check_type to a tuple by using a more direct not isinstance(check_type, type) check, which correctly handles all non-type iterables

Enhanced Output and List Utilities

  • list_columns(): Remove redundant @overload declarations and use Literal[True, False] for the print_output parameter to provide clearer intent to the caller
  • flatten(): Refine the input type to Collection[Collection[Any]] to more accurately represent the expected nested structure and remove unnecessary type: ignore comments

📖 Documentation and Metadata Improvements

Class and Method Documentation

  • Defaults(): Update the class docstring to include a dedicated "Methods" section, explicitly documenting the .get(), ._validate_value_and_default(), and ._validate_type() methods
  • retry(): Add descriptive docstrings for the _exceptions type alias and the R TypeVar to improve maintainability of the retry logic
  • Docstring Accuracy: Update parameter descriptions in multiple functions to reflect the shift from "tuple of types" to the more general "Sequence of types" or "Collection of types"

Module Exports

  • __all__: Ensure all modules have their __all__ lists correctly typed as list[str] and include newly relocated constants like log_levels in output.py

- Replace custom type aliases from the `toolbox_python.collection_types` module with standard library types like `list[str]`, `Collection[Any]`, and `Dict[Any, Any]`.
- Simplify `is_value_of_type()` and `assert_value_of_type()` function overloads by using `Collection[type]` to handle multiple input sequences.
- Relocate the `log_levels` type literal to `output.py` module to better align with its usage in output formatting.
- Add explicit `Callable` type hints to function aliases within the `checkers.py` module for improved type clarity.
- Update the `Defaults()` class docstring to include a dedicated methods section documenting the `.get()` method.
- Refactor the `flatten()` function and the `list_columns()` function to use standard collection types and remove redundant `@overload` definitions.
- Improve type checking logic in the `checkers.py` module to handle generic collections of types more robustly.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request standardizes type hints across the codebase by transitioning from custom type aliases (defined in collection_types.py) to standard Python typing constructs like Collection[Any], list[str], and dict[Any, Any]. The changes improve compatibility with modern static analysis tools and reduce internal coupling by eliminating numerous imports from the collection_types module.

Key changes include:

  • Replacing custom type aliases with standard library types (Collection, list, dict, Union, Literal)
  • Simplifying function overloads by consolidating type parameters
  • Relocating the log_levels constant from collection_types.py to output.py

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/toolbox_python/strings.py Replaced str_list, str_list_tuple aliases with list[str] and Union[list[str], tuple[str, ...]]
src/toolbox_python/retry.py Updated __all__ to list[str], added documentation for type variable and exception type alias
src/toolbox_python/output.py Relocated log_levels constant, removed custom collection type imports, simplified list_columns by removing overloads
src/toolbox_python/lists.py Changed return types from custom aliases to list[Any], refined input types to Collection[Collection[Any]]
src/toolbox_python/generators.py Updated __all__ type hint from str_list to list[str]
src/toolbox_python/dictionaries.py Replaced dict_any and dict_str_any with explicit dict[Any, Any] and dict[str, Any]
src/toolbox_python/defaults.py Updated __all__ type hint, added Methods section to class docstring, changed internal list type
src/toolbox_python/collection_types.py Removed log_levels constant (relocated to output.py)
src/toolbox_python/checkers.py Simplified overloads by consolidating to Collection[type], updated all type hints from custom aliases to standard types, added explicit Callable annotations to function aliases
src/toolbox_python/bools.py Updated __all__ type hint from str_list to list[str]

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/toolbox_python/checkers.py Outdated
Comment thread src/toolbox_python/checkers.py Outdated
Comment thread src/toolbox_python/checkers.py Outdated
Comment thread src/toolbox_python/checkers.py Outdated
Comment thread src/toolbox_python/checkers.py Outdated
Comment thread src/toolbox_python/checkers.py Outdated
Comment thread src/toolbox_python/checkers.py Outdated
chrimaho and others added 6 commits January 3, 2026 09:26
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
When accessing and formatting the type in the error message. This line should be `msg += f"Must be '{check_type.__name__}'"` to be consistent with line 647 in `assert_value_of_type()` and to produce cleaner error messages that show just the type name (e.g., `'int'`) rather than the full type object representation (e.g., `<class 'int'>`).

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@chrimaho chrimaho merged commit cc3b624 into main Jan 2, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants